package io.renren;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zheok.testing.entity.*;
import com.zheok.testing.vo.CatagoryVo;
import com.zheok.testing.vo.LableVo;
import com.zheok.testing.vo.ParamJsonVo;
import io.renren.modules.app.service.*;
import io.renren.modules.app.utils.JwtUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.service.ThymeleafSc;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Hash;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ThymeleafPage {
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private CatagoryService catagoryService;
    @Autowired
    private ProductService productService;
    @Autowired
    private JournalismService journalismService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private BannerPtService bannerPtService;
    @Autowired
    private BusinessService businessService;

    @Value("${shops_path}")
    private String shops_path;

    @Value("${business_path}")
    private String business_path;

    @Value("${index_path}")
    private String index_path;

    @Value("${product_path}")
    private String product_path;

    public void createShopPurchaseDetail() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<BusinessEntity> businesss = businessService.list();
        businesss.forEach(business -> {
            map.put("business", business);
            map.put("banners", banners);
            context.setVariables(map);
            //输出流
            File file = new File(business_path+"detail\\");

            file.mkdirs();
            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +business.getId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("businessDetail", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }

        });

    }

    public void createShopPurchase() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<BusinessEntity> businesss = businessService.list(null);
        map.put("businesss", businesss);
        map.put("banners", banners);
        context.setVariables(map);
        //输出流
        File file = new File(business_path);

        file.mkdirs();

        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "business.html"), "UTF-8")) {
            //生成html
            templateEngine.process("business", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }

    public void createProductList() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //2、平台联系方式
        //3、产品分类
        List<CatagoryVo> tree = catagoryService.tree();
        map.put("catagoryVos", tree);
        map.put("banners", banners);
        context.setVariables(map);
        //输出流
        File file = new File(index_path, "productList.html");
        try (PrintWriter printWriter = new PrintWriter(file, "UTF-8")) {
            //生成html
            templateEngine.process("productList", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }

    public void createShopActivity() {
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<SysUserEntity> shops = sysUserService.list();
        shops.forEach(shop -> {
            List<ActivityEntity> activitys = activityService.list(new QueryWrapper<ActivityEntity>().lambda()
                    .eq(ActivityEntity::getShopId, shop.getUserId()));
            map.put("activitys", activitys);
            map.put("shop", shop);
            context.setVariables(map);
            //输出流
            File file = new File(shops_path + shop.getUserId() + "\\activity\\");

            file.mkdirs();

            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +shop.getUserId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("shopActivity", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }
        });

    }

    public void createShopActivityDetail() {

        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<ActivityEntity> activitys = activityService.list();
        activitys.forEach(activity -> {
            SysUserEntity shop = sysUserService.getById(activity.getShopId());
            map.put("activity", activity);
            map.put("shop", shop);
            map.put("banners", banners);
            context.setVariables(map);
            if (shop != null) {
                //输出流
                File file = new File(shops_path + shop.getUserId() + "\\activity\\detail\\");

                file.mkdirs();

                try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +activity.getId() + ".html"), "UTF-8")) {
                    //生成html
                    templateEngine.process("activityDetail", context, printWriter);
                } catch (Exception e) {
                    log.error("[静态页服务] 生成静态页异常", e);
                }
            }

        });

    }

    public void createShopJournalismDetail() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<JournalismEntity> Journalisms = journalismService.list();
        Journalisms.forEach(Journalism -> {
            SysUserEntity shop = sysUserService.getById(Journalism.getShopId());
            map.put("journalism", Journalism);
            map.put("shop", shop);
            map.put("banners", banners);
            context.setVariables(map);
            if (shop != null) {
                //输出流
                File file = new File(shops_path + shop.getUserId() + "\\news\\detail\\");
                if (!file.isDirectory()) {
                    file.mkdir();
                }
                try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +Journalism.getId() + ".html"), "UTF-8")) {
                    //生成html
                    templateEngine.process("articleDetail", context, printWriter);
                } catch (Exception e) {
                    log.error("[静态页服务] 生成静态页异常", e);
                }
            }

        });

    }

    public void createShopJournalism() {     //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<SysUserEntity> shops = sysUserService.list();
        shops.forEach(shop -> {
            List<JournalismEntity> Journalisms = journalismService.list(new QueryWrapper<JournalismEntity>().lambda()
                    .eq(JournalismEntity::getShopId, shop.getUserId()));
            map.put("journalisms", Journalisms);
            map.put("shop", shop);
            map.put("banners", banners);
            context.setVariables(map);
            //输出流
            File file = new File(shops_path + shop.getUserId() + "\\news\\");
            file.mkdirs();
            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +shop.getUserId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("shopNews", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }
        });

    }

    public void createShopProduct() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<SysUserEntity> shops = sysUserService.list();
        shops.forEach(shop -> {
            List<ProductEntity> products = productService.list(new QueryWrapper<ProductEntity>().lambda()
                    .eq(ProductEntity::getShopId, shop.getUserId()));
            map.put("products", products);
            map.put("banners", banners);
            map.put("shop", shop);
            products.forEach(productEntity -> {
                //根据当前产品查询参数
                List<ParamJsonVo> param = productService.getParam(productEntity.getId() + "");
                productEntity.setParams(param);

            });
            context.setVariables(map);
            //输出流
            File file = new File(shops_path + shop.getUserId() + "\\product\\");

            file.mkdirs();

            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +shop.getUserId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("shopProduct", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }
        });

    }

    public void createProduct() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        //所有企业 new QueryWrapper<SysUserEntity>().lambda().ne(SysUserEntity::getUserId,"1")
        List<SysUserEntity> shops = sysUserService.list();
        //拿到所有产品

        //拿到所有二级分类
        List<CatagoryVo> tree = catagoryService.tree();
        tree.forEach(item -> {
            //一级分类
            item.getChildren().forEach(item2 -> {
                Map<String, Object> catagorys = new HashMap<>();
                catagorys.put("catagroy1", item.getLabel());//一级分类名称
                catagorys.put("catagroy2", item2.getLabel());//二级分类名称
                catagorys.put("catagroyId", item2.getId());//二级分类名称
                List<LableVo> catagory2 = productService.getCatagory2(item2.getId());
                catagorys.put("catagoryLables", catagory2);//二级分类标签
                catagorys.put("catagory3", item2.getChildren());
                catagorys.put("banners", banners);
                catagorys.put("shops", shops);//所有商户
                List<ProductEntity> products = productService.list(
                        new QueryWrapper<ProductEntity>().lambda()
                                .like(ProductEntity::getCatagoryId, item2.getId()));
                catagorys.put("products", products);//当前二级分类下的所有产品
                context.setVariables(catagorys);
                //根据分类ID生成产品页
                File file = new File(product_path, item2.getId() + ".html");
                try (PrintWriter printWriter = new PrintWriter(file, "UTF-8")) {
                    //生成html
                    templateEngine.process("product", context, printWriter);
                } catch (Exception e) {
                    log.error("[静态页服务] 生成静态页异常", e);
                }
            });

        });
    }

    public void createProductDetail() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //3、产品分类
        List<CatagoryVo> tree = catagoryService.tree();
        //4、新品 12 个
        List<ProductEntity> products = productService.list();
        products.forEach(item -> {
            map.put("product", item);
            map.put("catagoryVos", tree);
            SysUserEntity byId = sysUserService.getById(item.getShopId());
            map.put("shop", byId);
            map.put("banners", banners);
            //根据当前产品查询参数
            List<ParamJsonVo> param = productService.getParam(item.getId() + "");
            map.put("params", param);
            context.setVariables(map);
            //输出流
            File file = new File(product_path+"detail\\", item.getId() + ".html");
            try (PrintWriter printWriter = new PrintWriter(file, "UTF-8")) {
                //生成html
                templateEngine.process("detail", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }

        });

    }

    @Autowired
    ThymeleafSc thymeleafSc;

    @Test
    public void foawfa(){
        thymeleafSc.test();
    }

    public void createIndex() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //2、平台联系方式
        //3、产品分类
        List<CatagoryVo> tree = catagoryService.tree();
        //4、新品 12 个
        IPage<ProductEntity> page = new Page<>(1, 12);
        IPage<ProductEntity> productPgae = productService.page(page, null);
        //新闻 五个
        IPage<JournalismEntity> journalisms = journalismService.page(new Page<>(1, 5), null);
        //活动 四个
        IPage<ActivityEntity> activitys = activityService.page(new Page<>(1, 4), null);
        //采购
        IPage<BusinessEntity> business = businessService.page(new Page<>(1, 8), null);
        map.put("business", business.getRecords());
        map.put("activitys", activitys.getRecords());
        map.put("journalisms", journalisms.getRecords());
        map.put("products", productPgae.getRecords());
        map.put("catagoryVos", tree);
        map.put("banners", banners);
        context.setVariables(map);
        //输出流
        File file = new File(index_path, "index.html");
        try (PrintWriter printWriter = new PrintWriter(file, "UTF-8")) {
            //生成html
            templateEngine.process("index", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }

    @Test
    public void test() {
      thymeleafSc.test();
    }


}
